package com.example.springexcel.JavaFx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.net.URL;

public class DialogFile extends Application {
    public static Stage primaryStage;

    // 文件选择器选择文件路径
    public static File getShowOpenDialogFile() {
        FileChooser fc = new FileChooser();
        FileChooser.ExtensionFilter Excel = new FileChooser.ExtensionFilter("xlsx", "*.xlsx", "*.xls");


        fc.getExtensionFilters().addAll(Excel);
        fc.setTitle("请选择Excel文件");

        return fc.showOpenDialog(primaryStage);
    }

    // 目录选择器 选择文件夹
    public static File getDirectoryChooserSelectDirectory() {
        DirectoryChooser dc = new DirectoryChooser();
        dc.setTitle("选择一个文件夹,用于存放导出的文件");
        //设置初始目录
        dc.setInitialDirectory(new File("./"));
        return dc.showDialog(primaryStage);
    }
    // 对话框
    public static void AlertDialogBox(Alert.AlertType error, String ContentText, String HeaderText, String Title, EventHandler<ActionEvent> value) {
        Alert alert = new Alert(error);
        alert.setTitle(Title);
        alert.setHeaderText(HeaderText);
        alert.setContentText(ContentText);
        Button ok = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
        ok.setOnAction(value);
        alert.showAndWait();
    }



    @Override
    public void start(Stage stage) throws Exception {
        primaryStage = stage;
        URL resource = DialogFile.class.getResource("main.fxml");
        assert resource != null;
        FXMLLoader fxmlLoader = new FXMLLoader(resource);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

        //转化为舞台
        Scene scene = new Scene(fxmlLoader.load());

        //设置舞台
        stage.setScene(scene);
        //设置无法调整大小
        stage.setResizable(false);
        //设置标题
        stage.setTitle("Excel批量处理！！！");
        //显示舞台
        stage.show();
    }
}
